package com.ruoyi.web.controller.shop.shopuser.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.controller.shop.shop.domain.ShopShop;
import com.ruoyi.web.controller.shop.shop.service.IShopShopService;
import com.ruoyi.web.controller.shop.shopuser.domain.ShopRole;
import com.ruoyi.web.controller.shop.shopuser.domain.ShopUser;
import com.ruoyi.web.controller.shop.shopuser.service.IShopUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 店员信息Controller
 * 
 * @author ruoyi
 * @date 2020-10-30
 */
@RestController
@RequestMapping("/shopuser")
public class ShopUserController extends BaseController
{
    @Autowired
    private IShopUserService shopUserService;
    @Autowired
    private IShopShopService shopShopService;

    /**
     * 查询我添加的店员列表
     */
    @PostMapping("/user/list")
    public TableDataInfo userList(@RequestBody Map<String,Object> map)
    {
        if (!map.containsKey("shopId")){
            return getDataTable(null);
        }
        String shopId = map.get("shopId").toString();
        if (StringUtils.isEmpty(shopId)){
            return getDataTable(null);
        }
        startPage();
        List<Map<String,Object>> list = shopUserService.selectShopUserList(map);
        return getDataTable(list);
    }

    /**
     * 查询我添加的角色列表
     */
    @PostMapping("/role/list")
    public TableDataInfo roleList(@RequestBody Map<String,Object> map)
    {
        if (!map.containsKey("shopId")){
            return getDataTable(null);
        }
        String shopId = map.get("shopId").toString();
        if (StringUtils.isEmpty(shopId)){
            return getDataTable(null);
        }
        startPage();
        List<Map<String,Object>> list = shopUserService.selectShopRoleList(map);
        return getDataTable(list);
    }

    /**
     * 查询菜单列表
     */
    @PostMapping("/menu/list")
    public AjaxResult menuList(@RequestBody Map<String,Object> map)
    {
        if (!map.containsKey("shopId")){
            return AjaxResult.error("缺少店铺id");
        }
        String shopId = map.get("shopId").toString();
        if (StringUtils.isEmpty(shopId)){
            return AjaxResult.error("店铺不存在");
        }
        map.put("parentId","0");
        List<Map<String,Object>> list = shopUserService.selectShopMenuList(map);
        if (list.size()>0){
            for (Map<String,Object> m :list){
                map.put("parentId",m.get("menuId").toString());
                List<Map<String,Object>> l = shopUserService.selectShopMenuList(map);
                m.put("childList",l);
            }
        }
        return AjaxResult.success(list);
    }

    /**
     * 获取店员信息详细信息
     */
    @GetMapping(value = "/user/{userId}")
    public AjaxResult getUserInfo(@PathVariable("userId") Long suserId)
    {
        Map<String,Object> map = shopUserService.selectShopUserById(suserId);
        Map<String,Object> role = shopUserService.selectShopRoleByUserId(suserId);
        map.put("role",role);
        return AjaxResult.success(map);
    }

    /**
     * 获取店员信息详细信息
     */
    @PostMapping(value = "/user/repeat")
    public AjaxResult userRepeat(@RequestBody Map<String,String> map)
    {
        int total = shopUserService.countUserName(map.get("userName"));
        if (total>0){
            return AjaxResult.error("账号已存在");
        }else {
            return AjaxResult.success("账号可用");
        }
    }

    /**
     * 获取角色信息
     */
    @GetMapping(value = "/role/{roleId}")
    public AjaxResult getRoleInfo(@PathVariable("roleId") Long roleId)
    {
        ShopRole shopRole = shopUserService.selectShopRoleById(roleId);
        Map<String,Object> map = new HashMap<>();
        map.put("roleId",shopRole.getRoleId().toString());
        map.put("parentId","0");
        List<Map<String,Object>> list = shopUserService.queryMenuByRoleId(map);
        if (list.size()>0){
            for (Map<String,Object> m:list){
                map.put("parentId",m.get("menuId").toString());
                List<Map<String,Object>> l = shopUserService.queryMenuByRoleId(map);
                m.put("childList",l);
            }
        }
        shopRole.setMenuList(list);
        return AjaxResult.success(shopRole);
    }

    /**
     * 新增店员信息
     */
    @Log(title = "店员信息", businessType = BusinessType.INSERT)
    @PostMapping(value = "/user/add")
    public AjaxResult userAdd(@RequestBody ShopUser shopUser)
    {
        int total = shopUserService.countUserName(shopUser.getUserName());
        if (total>0){
            return AjaxResult.error("账号已存在");
        }
        return shopUserService.insertShopUser(shopUser);
    }

    /**
     * 新增角色信息
     */
    @Log(title = "角色信息", businessType = BusinessType.INSERT)
    @PostMapping(value = "/role/add")
    public AjaxResult roleAdd(@RequestBody ShopRole shopRole)
    {
        ShopShop shopShop = shopShopService.selectShopShopById(shopRole.getShopId());
        if (shopShop==null){
            return AjaxResult.error("店铺信息异常");
        }
        return shopUserService.insertShopRole(shopRole);
    }

    /**
     * 修改店员信息
     */
    @Log(title = "店员信息", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/user/edit")
    public AjaxResult userEdit(@RequestBody ShopUser shopUser)
    {
        return shopUserService.updateShopUser(shopUser);
    }

    /**
     * 重置店员密码
     */
    @Log(title = "店员信息", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/user/reset")
    public AjaxResult userReset(@RequestBody ShopUser shopUser)
    {
        shopUser.setPassword(SecurityUtils.encryptPassword(shopUser.getPassword()));
        return toAjax(shopUserService.resetPassword(shopUser));
    }

    /**
     * 修改角色信息
     */
    @Log(title = "角色信息", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/role/edit")
    public AjaxResult roleEdit(@RequestBody ShopRole shopRole)
    {
        return shopUserService.updateShopRole(shopRole);
    }

    /**
     * 删除店员信息
     */
    @Log(title = "店员信息", businessType = BusinessType.DELETE)
	@PostMapping("/user/remove")
    public AjaxResult userRemove(@RequestBody Map<String,Object> map)
    {
        return shopUserService.removeShopUser(map);
    }
    /**
     * 删除角色信息
     */
    @Log(title = "角色信息", businessType = BusinessType.DELETE)
    @PostMapping("/role/remove")
    public AjaxResult roleRemove(@RequestBody Map<String,Object> map)
    {
        return shopUserService.removeShopRole(map);
    }
}
